package com.atguigu.lease.web.admin.controller.apartment;


import com.atguigu.lease.common.result.Result;
import com.atguigu.lease.model.entity.AttrKey;
import com.atguigu.lease.model.entity.AttrValue;
import com.atguigu.lease.model.entity.LabelInfo;
import com.atguigu.lease.model.enums.ItemType;
import com.atguigu.lease.web.admin.service.AttrKeyService;
import com.atguigu.lease.web.admin.service.AttrValueService;
import com.atguigu.lease.web.admin.vo.attr.AttrKeyVo;
import com.baomidou.mybatisplus.core.conditions.query.LambdaQueryWrapper;
import io.swagger.v3.oas.annotations.Operation;
import io.swagger.v3.oas.annotations.tags.Tag;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.*;

import java.util.List;


@Tag(name = "房间属性管理")
@RestController
@RequestMapping("/admin/attr")
public class AttrController {

    @Autowired
    private AttrKeyService attrKeyService;

    @Autowired
    private AttrValueService attrValueService;


    @Operation(summary = "新增或更新属性名称")
    @PostMapping("key/saveOrUpdate")
    public Result saveOrUpdateAttrKey(@RequestBody AttrKey attrKey) {
        boolean saveOrUpdate = attrKeyService.saveOrUpdate(attrKey);
        return saveOrUpdate? Result.ok(): Result.fail();
    }

    @Operation(summary = "新增或更新属性值")
    @PostMapping("value/saveOrUpdate")
    public Result saveOrUpdateAttrValue(@RequestBody AttrValue attrValue) {
        boolean saveOrUpdate = attrValueService.saveOrUpdate(attrValue);
        return saveOrUpdate? Result.ok(): Result.fail();
    }

    /*
    需求是：查询所有房间的属性名称和属性值列表。

    实现方法：改造接收VO
            定义service方法
            定义mapper方法
            定义mapperxml多表查询+resultMap结果映射
    问题：1.返回的结果单个实体类无法完成映射
            AttrKey->进行改造List<AttrValue> attrValueList mybatis-plus自动完成映射
            创建一个Vo ->用于接收或者返回本次的结果 -> AttrVo extends AttrKey + List<AttrValue> attrValueList
           2.本次多表查询是两个表的外连接查询需要自定义mybatis多表查询
           3.自定义sql查询 关注逻辑字段 is_delete=0
           4.使用外连接查询



     */


    @Operation(summary = "查询全部属性名称和属性值列表")
    @GetMapping("list")
    public Result<List<AttrKeyVo>> listAttrInfo() {
        List<AttrKeyVo> list = attrKeyService.customList();
        return Result.ok(list);
    }


    //删除主表信息attr_key，也应该删除对应子表信息attr_value,否则会有孤儿产生。

    @Operation(summary = "根据id删除属性名称")
    @DeleteMapping("key/deleteById")
    public Result removeAttrKeyById(@RequestParam Long attrKeyId) {

        //根据ID删除属性名称
        boolean removeById = attrKeyService.removeById(attrKeyId);
        //如果删除了主表的一条数据，子表的数据需要一并删除，否则孤儿产生。
        //子表根据主表的ID删除(attr_kry_id)删除多条数据。
        if (removeById){
            LambdaQueryWrapper<AttrValue> lambdaQueryWrapper = new LambdaQueryWrapper<>();
            lambdaQueryWrapper.eq(AttrValue::getAttrKeyId,attrKeyId);
            attrValueService.remove(lambdaQueryWrapper);
            return Result.ok();
        }
        return Result.fail();
    }

    @Operation(summary = "根据id删除属性值")
    @DeleteMapping("value/deleteById")
    public Result removeAttrValueById(@RequestParam Long id) {
        attrValueService.removeById(id);
        return Result.ok();
    }

}
